<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
     <!-- :disabled="mode=='show'" -->
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px">
      <!-- <el-form-item label="ID" prop="id">
        <el-input v-model="form.id" placeholder="ID" clearable :readonly="mode=='show'"></el-input>
      </el-form-item> -->
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="名称" clearable :readonly="mode=='show'"></el-input>
      </el-form-item>
      <el-form-item label="分组" prop="groupName">
        <el-input v-model="form.groupName" placeholder="分组" clearable :readonly="mode=='show'"></el-input>
      </el-form-item>
      <el-form-item label="节点" prop="nodeName">
        <el-input v-model="form.nodeName" placeholder="节点" clearable :readonly="mode=='show'"></el-input>
      </el-form-item>
      <el-form-item label="字符值" prop="vs">
        <el-input v-model="form.vs" placeholder="字符值" clearable :readonly="mode=='show'"></el-input>
      </el-form-item>
      <el-form-item label="数值" prop="vi">
        <el-input v-model="form.vi" placeholder="数值" clearable :readonly="mode=='show'"></el-input>
      </el-form-item>
      <el-form-item label="浮点值" prop="vd">
        <el-input v-model="form.vd" placeholder="浮点值" clearable :readonly="mode=='show'"></el-input>
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
		<el-date-picker type="datetime" :format="this.$CONFIG.DATE_TIME_FORMAT_TEXT" :value-format="this.$CONFIG.DATE_TIME_FORMAT_VALUE" v-model="form.expireTime" placeholder="过期时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime" v-if="mode=='show'">
		<el-date-picker type="datetime" :format="this.$CONFIG.DATE_TIME_FORMAT_TEXT" :value-format="this.$CONFIG.DATE_TIME_FORMAT_VALUE" v-model="form.updateTime" placeholder="更新时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="mode=='show'">
		<el-date-picker type="datetime" :format="this.$CONFIG.DATE_TIME_FORMAT_TEXT" :value-format="this.$CONFIG.DATE_TIME_FORMAT_VALUE" v-model="form.createTime" placeholder="创建时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="备注" clearable :readonly="mode=='show'"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button v-if="mode!='show'" @click="visible=false" >取 消</el-button>
      <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
    import setting from './setting'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: null,
					groupName: null,
					name: null,
					nodeName: null,
					vs: null,
					vi: null,
					vd: null,
					createTime: null,
					expireTime: null,
					updateTime: null,
					remark: null,
					_: ""
				},
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入名称'}
					]
				},
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == "edit"){
							res = await setting.api.edit.post(this.form)
						} else {
							res = await setting.api.add.post(this.form)
						}
						this.isSaveing = false;
						if(res.code == 0){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.msg, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.groupName = data.groupName
				this.form.name = data.name
				this.form.nodeName = data.nodeName
				this.form.vs = data.vs
				this.form.vi = data.vi
				this.form.vd = data.vd
				this.form.createTime = data.createTime
				this.form.expireTime = data.expireTime
				this.form.updateTime = data.updateTime
				this.form.remark = data.remark

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
